

<template>
<!-- 面包屑 -->
  <div>
    <el-breadcrumb :separator-icon="ArrowRight">
      <el-breadcrumb-item>首页</el-breadcrumb-item>
      <el-breadcrumb-item>信息管理</el-breadcrumb-item>
      <el-breadcrumb-item><b>院系管理</b></el-breadcrumb-item>
    </el-breadcrumb>
  </div>
  <div>
    <el-button type="success" @click="dialogAddState=true" >
      <el-icon><Plus /></el-icon>  添加
    </el-button>
  </div>
  <!--  数据表格-->
  <div style="margin:20px">
    <el-table :data="deptInfo" stripe border style="width: 100%">
      <el-table-column prop="deptid" label="院系编号" v-if="show" />
      <el-table-column prop="deptno" label="院系编号" />
      <el-table-column prop="deptname" label="院系名称"/>
      <el-table-column label="操作" width="160">
        <template #default="scope">
          <el-button
              size="small"
              type="primary"
              @click="tableEditHandler(scope.$index, scope.row)">
            <el-icon><Edit /></el-icon>编辑
          </el-button>
          <el-button
              size="small"
              type="danger"
              @click="tableDeleteHandler(scope.$index, scope.row)">
            <el-icon><Delete /></el-icon>删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
  <!--添加 -->
  <el-dialog v-model="dialogAddState" title="添加院系信息" width="380">
    <div>
      <el-form ref="formAddRef" :rules="formAddRules" :model="formAdd" label-width="120px">
        <el-form-item prop="deptid">
          <el-input v-model="formAdd.deptid" v-show="show"></el-input>
        </el-form-item>
        <el-form-item prop="deptno" label="院系编号">
          <el-input v-model="formAdd.deptno" maxlength="5" show-word-limit></el-input>
        </el-form-item>
        <el-form-item prop="deptname" label="院系名称">
          <el-input v-model="formAdd.deptname" maxlength="8" show-word-limit></el-input>
        </el-form-item>
      </el-form>
    </div>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogAddOnReset">取消</el-button>
        <el-button type="primary"  @click="dialogAddOnSubmit" >确认</el-button>
      </div>
    </template>
  </el-dialog>
<!-- 编辑 -->
  <el-dialog v-model="dialogEditState" title="修改院系信息" width="380">
    <div>
      <el-form ref="formEditRef" :model="formEdit"
               :rules="formEditRules"
               label-width="120px">
        <el-form-item prop="deptno" label="院系编号">
          <el-input v-model="formEdit.deptno"  disabled></el-input>
        </el-form-item>
        <el-form-item prop="deptname" label="院系名称">
          <el-input v-model="formEdit.deptname" maxlength="8" ></el-input>
        </el-form-item>
      </el-form>
    </div>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogEditOnReset">取消</el-button>
        <el-button type="primary"  @click="dialogEditOnSubmit" >确认</el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script setup>

import {ArrowRight, Delete, Edit, Plus} from "@element-plus/icons-vue";
import {onMounted, reactive, ref} from "vue";
import request from "../../../utils/freshman";
import {ElMessage, ElMessageBox} from "element-plus";

const deptInfo=ref([]);
onMounted(()=>{
  getDept();
})
const getDept=()=>{
  request({
    url:'/dept/all',
    method:'get'
  }).then((req)=>{
    console.log(req)
    if (200==req.data.errorCode){
      deptInfo.value=req.data.data;
    }
  })
}

/////编辑
const formEditRef = ref()
const formEditData = {
  deptid:'',
  deptno:'',
  deptname:''
}
const formEditRules={
  deptname: [
    { required: true, message: '请输入院系名称', trigger: 'blur' },
    { pattern: /^[\u4e00-\u9fa5]+$/,message: '院系名称必须为中文', trigger: 'blur' }
  ]
};
// 编辑表单绑定数据模型
const formEdit = reactive({...formEditData})
const tableEditHandler = (rowIndex , rowData) => {
  console.log("-------------")
  console.log(rowIndex)
  console.log(rowData)
  formEdit.deptid = rowData.deptid
  formEdit.deptno = rowData.deptno
  formEdit.deptname = rowData.deptname
  dialogEditState.value = true
}
const dialogEditState = ref(false)
const dialogEditOnSubmit = () => {
  request({
    url:'/dept/update',
    method:'put',
    data:JSON.stringify(formEdit),
    headers:{'content-type' : "application/json;charset=UTF-8"}
  }).then(req=>{
    if (200==req.data.errorCode){
      ElMessage({
        type: 'success',
        message: '修改成功！',
      });
      getDept();
    }
  })
  dialogEditState.value = false;
}
const dialogEditOnReset = () => {
  Object.assign(formEdit , formEditData)
  dialogEditState.value = false
}
/////////////删除////////////////
//删除一条
const tableDeleteHandler = (rowIndex , rowData) => {
  ElMessageBox.confirm(
      '确认删除 : ' +rowData.deptid,
      'Warning',
      {
        confirmButtonText: '删除',
        cancelButtonText: '取消',
        type: 'warning',
      }
  ).then(() => {//点击了删除按钮
    request({
      method:'delete',
      url:'/dept/delete/'+rowData.deptid
    }).then(req=>{
      if (200==req.data.errorCode){
        ElMessage({
          type: 'success',
          message: '删除成功',
        })
        getDept();
      }
    })
  })
      .catch(() => {//点击了取消按钮
        ElMessage({
          type: 'info',
          message: '取消删除',
        })
      })
}

//////////////////添加////////////////////
const dialogAddState = ref(false)
const formAddRules={
  deptno: [
    { required: true, message: '院系编号不能为空', trigger: 'blur' },
    { pattern: /^\d+$/, message: '院系编号只能为数字', trigger: 'blur' }
  ],
  deptname: [
    { required: true, message: '负责院系不能为空', trigger: 'change' },
    { pattern: /^[\u4e00-\u9fa5]+$/, message: '院系名称必须为中文', trigger: 'blur' }
  ]
};
const dialogAddOnSubmit = () => {
  request({
    method:'post',
    url:'/dept/insert',
    params:formAdd,
  }).then(req=>{
    if (200 == req.data.errorCode){
      ElMessage({
        type:'success',
        message:req.data.msg
      })
      Object.assign(formAdd , formAddData)
      getDept();
    }
  })
  dialogAddState.value=false;
}
const dialogAddOnReset = () => {
  // 清空表单数据
  Object.assign(formAdd , formAddData)
  // 关闭对话框
  dialogAddState.value = false
}
// 新增表单引用
const formAddRef = ref()
// 新增表单初始化数据
const formAddData = {
  deptid:'',
  deptno:'',
  deptname:''
}
// 新增表单绑定数据模型
const formAdd = reactive({...formAddData})   // ...formAddData 解构formAddData
</script>
<style scoped>
.el-breadcrumb{
  font-size: 18px;
  margin: 20px;
}
</style>